﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace 自动出题并判分
{
    public partial class Form1 : Form
    {
        int sec = 0;               //计时数字
        int score = 0;             //分数统计
        int first=0;               //第一个数字
        int second=0;              //第二个数字
        int interval_timer = 1000; //自动显示间隔
        bool isOn = true;         //开始标记
        int mode = 0;              //加减标志
        int flag1 = 0;             //计时控制位
       

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckResult())
            {
                score++;
                lab_score.Text = score.ToString();
            }
            text_result.Text = null;

            text_result.Enabled = true;
            text_result.Focus();
            lab_time.Enabled = true;
            button2.Visible = true;

            //题目计时开始
            timer1.Interval = interval_timer;
            
            timer1.Start();
           
            
            if(isOn)
            {
                start();
                button1.Text = "下一题";
            }
            else
            {
                isOn = true;
                /*button1.Width = 50;
                button1.Visible = true;
                */
                return;
            }
           

        }

        

       
        //结束按钮按下效果
        private void button2_Click(object sender, EventArgs e)
        {
            isOn = false;
            text_result.Enabled = false;
            button1.Width = 112;
            lab_score.Text = "0";
            button1.Text = "开始";
            button2.Visible = false;
            
            

            //秒数归零，停止计时
            lab_time.Enabled = false;
            lab_time.Text = "0s";
            timer1.Stop();
            timer2.Stop();

            MessageBox.Show("本次得分：" + score.ToString() + "\n" + "用时" + sec + "秒");
            score = 0;
            sec = 0;

        }

       
        //计时器的控制
        private void timer1_Tick(object sender, EventArgs e)
        {
            flag1++;
            sec++;
            lab_time.Text = sec + "s";

            //控制10秒跳转下一题
            if(flag1%10==0)
            {
                start();
            }
        }

       


        //开始函数,方便后续增添功能,可以添加Switch分支
        private void start()
        {
            Mode_plus_minus();
            flag1 = 0;           
        }

        //十进制加减法功能
        private void Mode_plus_minus()
        {
            Random rd = new Random();

            //随机生成100以内的数字
            num1.Text = rd.Next(1, 100).ToString();
            num2.Text = rd.Next(1, 100).ToString();

            first = int.Parse(num1.Text);
            second = int.Parse(num2.Text);

            //随机选择加减模式
            mode = rd.Next(2);
            switch (mode)
            {
                case 0:
                    lab_mode.Text = "+";
                    break;
                case 1:
                    lab_mode.Text = "-";
                    second *= -1;
                    break;
            }



        }

       

        private bool CheckResult()
        {
            int result = 1;
            try
            {
                result = int.Parse(text_result.Text);
            }
            catch (Exception e)
            {
                MessageBox.Show("输入不能为空！");
            }
            if(result==first+second)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        
    }
}
/*控制台程序*/
/* //Random类的调用
            Random random = new Random();
            int score = 0;

            for (int i = 0; i < 10; i++)
            {
                //随机生成两个1-9999的整数
                int num1 = random.Next(1, 10000);
                int num2 = random.Next(1, 10000);

                //随机生成加减两种操作
                int operation = random.Next(2);

                int result;
                string operatorSymbol;

                if (operation == 0)
                {
                    result = num1 + num2;
                    operatorSymbol = "+";
                }
                else
                {
                    result = num1 - num2;
                    operatorSymbol = "-";
                }

                Console.Write($"第{i + 1}题：{num1}{operatorSymbol}{num2}=");
                int userResult = Convert.ToInt32(Console.ReadLine());

                //判断用户输入是否正确
                if(userResult==result)
                {
                    Console.WriteLine("回答正确！\n");
                    score += 10;
                }
                else
                {
                    Console.WriteLine($"回答错误！正确答案为{result}\n");
                }

            }

            Console.WriteLine($"总得分为：{score}");
*/